package com.gcb.controller.project;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.PageResult;
import com.gcb.common.core.domain.R;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.domain.dto.ProjectQueryDTO;
import com.gcb.domain.dto.ProjectSaveDTO;
import com.gcb.domain.vo.ProjectVO;
import com.gcb.framework.utils.SecurityUtils;
import com.gcb.service.IProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目管理
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private IProjectService projectService;

    /**
     * 分页条件查询项目列表
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platfrom:project:page')")
    @PostMapping(value = "/page")
    public PageResult getPageList(@RequestBody ProjectQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<ProjectVO> pageList = projectService.getPageList(dto);
        return PageResult.ok(pageList);
    }


    /**
     * 根据企业获取项目列表(下拉列表)
     *
     * @param companyId 企业id
     * @return 项目列表
     */
    @GetMapping(value = "/list/by/companyId")
    public R<List<ProjectVO>> getListByCompanyId(@RequestParam Long companyId) {
        List<ProjectVO> pageList = projectService.getListByCompanyId(companyId);
        return R.ok(pageList);
    }

    /**
     * 402.发布项目列表接口
     *
     * @param projectDto projectDto
     * @return AjaxResult
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platfrom:project:add')")
    @PostMapping("add")
    public R<String> addProject(@RequestBody ProjectSaveDTO projectDto) {
        projectService.save(projectDto, SecurityUtils.getUserId());
        return R.ok();
    }

    /**
     * 编辑项目列表接口
     *
     * @param projectDto projectDto
     * @return AjaxResult
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platfrom:project:edit')")
    @PostMapping("edit")
    public R<String> updateProject(@RequestBody ProjectSaveDTO projectDto) {
        projectService.update(projectDto, SecurityUtils.getUserId());
        return R.ok();
    }

    /**
     * 查询项目详情接口
     *
     * @param id 项目id
     * @return Result
     */
    @PreAuthorize("@ss.hasPermi('platfrom:project:detail')")
    @GetMapping("detail")
    public R<ProjectVO> getDetail(@RequestParam(name = "id") String id) {
        ProjectVO vo = projectService.getDetail(id);
        return R.ok(vo);
    }

    /**
     * 导出项目接口
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platfrom:project:export')")
    @PostMapping("export")
    public void exportList(HttpServletResponse response, ProjectQueryDTO dto) {
        List<ProjectVO> pageList = projectService.getPageList(dto);
        ExcelUtil<ProjectVO> util = new ExcelUtil<>(ProjectVO.class);
        util.exportExcel(response, pageList, "项目列表");
    }
}
